package com.stylefeng.guns.modular.system.controller;

import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.model.Area;
import com.stylefeng.guns.common.persistence.model.Village;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.VillageDao;
import com.stylefeng.guns.modular.system.warpper.VillageWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 小区控制器
 *
 * @author fengshuonan
 * @Date 2017年2月17日20:27:22
 */
@Controller
@RequestMapping("/village")
public class VillageController extends BaseController {

    private String PREFIX = "/system/village/";

    @Resource
    private VillageDao villageDao;

    /**
     * 跳转到小区管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "village.html";
    }

    /**
     * 跳转到添加小区页面
     */
    @RequestMapping("/village_add")
    public String villageAdd() {
        return PREFIX + "village_add.html";
    }

    /**
     * 跳转到修改小区页面
     */
    @RequestMapping("/village_update/{id}")
    public String villageUpdate(@PathVariable Integer id, Model model) {
        Village village = villageDao.selectById(id);
        model.addAttribute("village",village);
        LogObjectHolder.me().set(village);
        return PREFIX + "village_edit.html";
    }

    /**
     * 新增小区
     */
    @BussinessLog(value = "添加小区", key = "simplename", dict = Dict.DeptDict)
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Village village) {
        if (ToolUtil.isOneEmpty(village, village.getVil_name())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return this.villageDao.insert(village);
    }

    /**
     * 修改小区
     */
    @BussinessLog(value = "修改小区", key = "simplename", dict = Dict.DeptDict)
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object update(Village village) {
        if (ToolUtil.isEmpty(village) || village.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        villageDao.updateById(village);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除小区
     */
    @BussinessLog(value = "删除小区", key = "deptId", dict = Dict.DeleteDict)
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer id) {

        //缓存被删除的小区名称
        LogObjectHolder.me().set(villageDao.getVilNameById(id));

        villageDao.deleteById(id);

        return SUCCESS_TIP;
    }

    /**
     * 获取小区列表
     */
    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) String vil_name) {
        List<Map<String, Object>> list = this.villageDao.list(vil_name);
        return super.warpObject(new VillageWarpper(list));
    }

    /**
     * 获取省
     */
    @RequestMapping(value = "/GetProvice")
    @ResponseBody
    public String GetProvice(){
        List<Area> list = this.villageDao.GetProvice();
        String json=JSON.toJSON(list).toString();
        return json;
    }

    /**
     * 获取市
     */
    @RequestMapping(value = "/GetCity")
    @ResponseBody
    public String GetCity(Integer parentiD){
        List<Area> list = this.villageDao.GetCity(parentiD);
        String json=JSON.toJSON(list).toString();
        return json;
    }

    /**
     * 获取县
     */
    @RequestMapping(value = "/GetCounty")
    @ResponseBody
    public String GetCounty(Integer parentiD){
        List<Area> list = this.villageDao.GetCounty(parentiD);
        String json=JSON.toJSON(list).toString();
        return json;
    }

    /**
     * 获取乡镇
     */
    @RequestMapping(value = "/GetStreet")
    @ResponseBody
    public String GetStreet(Integer parentiD){
        List<Area> list = this.villageDao.GetStreet(parentiD);
        String json=JSON.toJSON(list).toString();
        return json;
    }
}
